﻿using UnityEngine;
using System.Collections;

public interface INGUIEvent{
	GameObject getGameObject {
		get;
	}
}
public struct NGUIAutoNotifyData{
	public GameObject obj;
	public int index;
	public bool isPress;
	public string funName;
}
public class NGUIEx
{
	static public  bool isplaysound = true;
	public static void AutoNotifyDrag(GameObject go){
		if (go == null)
			return;
		INGUIEvent dlg = go.GetComponentInParent<INGUIEvent> ();
		if (dlg == null)
			return;
		string sendname = "";
		NGUIAutoNotifyData data = new NGUIAutoNotifyData ();
		if (go.name.StartsWith ("item_", System.StringComparison.Ordinal)) {
			string[] names = go.name.Split ('_');
			data.index = int.Parse (names [2]);
			data.obj = go;
			sendname = names [1];
		} else if(go.name.StartsWith ("btn_", System.StringComparison.Ordinal)) {
			string[] names = go.name.Split ('_');
			data.obj = go;
			sendname = names [1];
		}
		if (sendname != "") {
			data.funName = sendname;
			SendMsg(dlg.getGameObject,"OnDrag",data);
//			dlg.gameObject.SendMessage (string.Format ("Drag_{0}", sendname), data, SendMessageOptions.DontRequireReceiver);
		}
	}
	public static void AutoNotifyPress(GameObject go,bool isPress){
		if (go == null)
			return;
		INGUIEvent dlg = go.GetComponentInParent<INGUIEvent> ();
		if (dlg == null)
			return;
		string sendname = "";
		NGUIAutoNotifyData data = new NGUIAutoNotifyData ();
		if (go.name.StartsWith ("item_", System.StringComparison.Ordinal)) {
			string[] names = go.name.Split ('_');
			data.index = int.Parse (names [2]);
			data.obj = go;
			sendname = names [1];
		} else if(go.name.StartsWith ("btn_", System.StringComparison.Ordinal)) {
			string[] names = go.name.Split ('_');
			data.obj = go;
			sendname = names [1];
		}
		if (sendname != "") {
			data.isPress = isPress;
			data.funName = sendname;
			SendMsg(dlg.getGameObject,"OnPress",data);
//			dlg.gameObject.SendMessage (string.Format ("Press_{0}", sendname), data, SendMessageOptions.DontRequireReceiver);
		}
	}
    public static void AutoNotifyBtnClick(GameObject go)
    {
		if (NGUITools.GetActive (go)) {
			if (go.name.StartsWith ("btn_", System.StringComparison.Ordinal)) {
				INGUIEvent dlg = go.GetComponentInParent<INGUIEvent> ();
				if (dlg != null) {
					NGUIAutoNotifyData data = new NGUIAutoNotifyData ();
					data.obj = go;
					string sendname = go.name.Remove(0, 4);
					data.funName = sendname;
					SendMsg(dlg.getGameObject,"OnClick",data);
//					dlg.gameObject.SendMessage (string.Format ("Click_{0}", str), data, SendMessageOptions.DontRequireReceiver);

				}
			} else if (go.name.StartsWith ("item_", System.StringComparison.Ordinal)) {
				INGUIEvent dlg = go.GetComponentInParent<INGUIEvent> ();
				if (dlg != null) {
					string[] names = go.name.Split ('_');
					NGUIAutoNotifyData data = new NGUIAutoNotifyData ();
					data.obj = go;
					data.index = int.Parse (names [2]);
					data.funName = names[1];
					SendMsg(dlg.getGameObject,"OnClickItem",data);

//					dlg.gameObject.SendMessage (string.Format ("Click_{0}", names[1]), data, SendMessageOptions.DontRequireReceiver);

				}    
			}
		}
    }

	public static void SendMsg(GameObject target,string motedname,NGUIAutoNotifyData data){
		if(target == null)
			return;
		target.SendMessage(motedname,data);
	}
	public static void AutoNodifyToggle(GameObject go)
	{
		if(NGUITools.GetActive(go))
		{
			INGUIEvent dlg = go.GetComponentInParent<INGUIEvent>();
			if(dlg != null)
			{
				dlg.getGameObject.SendMessage(string.Format("Toggle_{0}", go.name), go, SendMessageOptions.DontRequireReceiver);
			}
		}
	}
	public void isntplaySound(){
		isplaysound = false;
	}
}
